package com.pb.infra.taskManage.service;

import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.taskManage.mapper.TaskRelMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *任务依赖关系
 * @author liaohaoyuan
 * @date 20210501
 */
@Service
public class TaskRelService {

    /**
     * DAO映射对象
     */
    @Resource
    private TaskRelMapper taskRelMapper;

    /**
     * 查询分页数据
     * @param map 查询参数
     * @param page 页码
     * @return
     */
    public Result select(Map<String, Object> map, Page page){
        Integer pageIndex = Integer.parseInt(map.get("pageIndex").toString());
        Integer pageSize = Integer.parseInt(map.get("pageSize").toString());
        Integer pageEnd = pageSize * pageIndex;
        Integer pageStart = (pageIndex - 1) * pageSize;
        map.put("pageEnd", pageEnd);
        map.put("pageStart", pageStart);
        String groupName=(String)map.get("groupName")==null?"1":(String)map.get("groupName");
        map.put("groupName",groupName);
        List<Map<String, Object>> list = taskRelMapper.select(map);
        return Result.ok(taskRelMapper.selectCount(map), list);
    }

    /**
     * 获取配置表中左表数据
     * @param paramMap
     * @return
     */
    public Result getLeftTableData(Map<String,Object> paramMap){
        List<Map<String, Object>> list = taskRelMapper.getLeftTableData(paramMap);
        return Result.ok(list);
    }

    /**
     * 获取配置表右表数据
     * @param paramMap
     * @return
     */
    public List<Map<String,Object>> getRightTableData(Map<String,Object> paramMap){
        List<Map<String,Object>> list=taskRelMapper.getRightTableData(paramMap);
        return list;
    }

    /**
     * 保存配置表右表数据
     * @param list
     */
    @Transactional
    public void saveTables(List<Map<String,Object>> list){
        Map<String,Object> param=new HashMap<String,Object>();
        for (Map<String,Object> map : list){
            param.put("jobId",map.get("jobId"));
            param.put("relySysNo",map.get("relySysNo"));
        }
        // 先全部删掉，再存储所有
        taskRelMapper.delete(param);
        for (Map<String,Object> map : list){
            taskRelMapper.saveTable(map);
        }
    }

}
